<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Fenye{
	var $base_url			= ''; // 路径
	var $params             ='';  //参数
	var $prefix				= ''; // A custom prefix added to the path.
	var $suffix				= ''; // 后缀
	var $total_rows			=  0; // 总记录数
	var $per_page			= 10; // 一页显示条数
	var $num_links			=  2; // Number of "digit" links to show before/after the currently viewed page
	var $cur_page			=  0; // 当前页
	var $use_page_numbers	= TRUE; // 是否开启页面显示
	var $first_link			= '«';
	var $next_link			= '>';
	var $prev_link			= '<';
	var $last_link			= '»';
	var $uri_segment		= 3;
	var $page_query_string	= FALSE;
	var $display_pages		= TRUE;
	var $anchor_class		= '';
	var $split_tag		    = '_';
	var $count_page         =FALSE;
	
	function initialize($params = array()){
		if (count($params) > 0){
			foreach ($params as $key => $val){
				if (isset($this->$key)){
					$this->$key = $val;
				}
			}
		}
	}
	//创建分页
	public function fenye(){  
	   $this->cur_page =max($this->cur_page,1); 
	   $totalpage  =ceil($this->total_rows/$this->per_page);
	   $rangepage  =4; 
	   $startpage  =max(1,$this->cur_page - $rangepage); 
	   $endpage    =min($totalpage,$startpage+$rangepage*2 - 1); 
	   $startpage  =min($startpage,$endpage - $rangepage*2 + 1); 
	   if($startpage < 1) $startpage = 1; 
	   $arr=explode($this->split_tag, $this->params);
	   $arr[count($arr)-1]=1; $this->params=implode($this->split_tag, $arr);//echo $this->params;
	   $fileext = $this->suffix ? $this->suffix:'.html'; 
	   //$this->params=$this->base_url.$this->params;
	   $url=$this->base_url.$this->params;
	   $html = ''; 
	   $html .= '<ul class="pagination "><li><a href="'.$this->base_url.$this->params.$fileext.'" class=" gradient">'.$this->first_link.'</a></li></ul>'; 
	   $arr[count($arr)-1]=$this->cur_page-1; $this->params=implode($this->split_tag, $arr);
	   $html .= $this->cur_page > 1 ? '<ul class="pagination"><li><a href="'.$this->base_url.$this->params.$fileext.'" class="page gradient">'.$this->prev_link.'</a></li></ul>':''; 
	   if($this->use_page_numbers){
	   		$html.='<ul class="pagination pagination-group">';
		   for($i = $startpage;$i <= $endpage;$i++){ 
		   	   $arr[count($arr)-1]=$i; $this->params=implode($this->split_tag, $arr);
		       $html .= '<li><a href="'.$this->base_url.$this->params.$fileext.'"'.($this->cur_page == $i ? ' class="active"':'class="page"').'>'.$i.'</a></li>'; 
		       if($i == $totalpage) break; 
		   } 
		   $html.='</ul>';
	   }
	   $arr[count($arr)-1]=$this->cur_page+1; $this->params=implode($this->split_tag, $arr);
	   $html .= $this->cur_page < $totalpage ? '<ul class="pagination"><li><a href="'.$this->base_url.$this->params.$fileext.'" class="page gradient">'.$this->next_link.'</a></li></ul>':''; 
	   $arr[count($arr)-1]=$totalpage; $this->params=implode($this->split_tag, $arr);
	   $html .= '<ul class="pagination"><li><a href="'.$this->base_url.$this->params.$fileext.'" class="gradient">'.$this->last_link.'</a></li></ul>'; 
	   
	   if($this->count_page) $html .= '第'.$this->cur_page.'页/共'.$totalpage.'页'; 
	   return $html; 
   } 
	
}